package com.ysstech.system.controller;

import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.system.service.DataQLService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
@Api(tags = "DataQL管理api")
@RequestMapping(value = "/dataql")
public class DataQLController extends BaseController {
    @Autowired
    private DataQLService dataQLService;

    @YssPermissions(value = {"system:dataql"})
    @ApiOperation(value = "跳转DataQL管理页面")
    @YssLog(module = "DataQL管理", remark = "跳转DataQL管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "system/dataql";
    }


    @YssPermissions(value = {"system:dataql"})
    @ApiOperation(value = "获取数据库表字段")
    @YssLog(module = "DataQL管理", remark = "获取数据库表字段")
    @RequestMapping(value = "/getColumns")
    @ResponseBody
    public RestResult getcolumns(String sql) throws Exception {
        return ResultGenerator.getSuccessResult("获取查询语句字段成功！", dataQLService.getTableColumnNames(sql));
    }

    @YssPermissions(value = {"system:dataql:search"})
    @ApiOperation(value = "通过SQL查询数据")
    @YssLog(module = "DataQL管理", remark = "通过SQL查询数据")
    @RequestMapping(value = "/queryData")
    @ResponseBody
    public RestResult queryData(String sql, String page, String rows) throws Exception {
        return dataQLService.queryData(sql, this.getPage(page, rows));
    }


    @YssPermissions(value = {"system:dataql:update"})
    @ApiOperation(value = "执行修改操作")
    @YssLog(module = "DataQL管理", remark = "执行修改操作")
    @RequestMapping(value = "/updateData")
    @ResponseBody
    public RestResult updateData(String sql) throws Exception {
        return dataQLService.updateData(sql);
    }

}
